package com.geek.gkfood.web.demo;

import com.geek.gkfood.framework.mq.RocketMQSender;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.stream.messaging.Source;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("demo")
public class DemoController {

    @Autowired
    @Qualifier(Source.OUTPUT)
    private MessageChannel snatchOrderSendChannel;

    @Setter
    @Getter
    @ToString
    public static class Info implements Serializable {
        @NotBlank
        String name;
        @NotBlank
        String age;
    }

    @GetMapping("/hello/{id}")
    public Object hello(@PathVariable String id) {
        String msg = "DemoController recv args: id=" + id + ", date="
                + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS").format(new Date());
        return msg;
    }

    @GetMapping("/hello2/{id}")
    public Object hello2(@PathVariable String id) {
        String msg = "DemoController recv args: id=" + Integer.parseInt(id) / 0 + ", date="
                + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS").format(new Date());
        return msg;
    }

    @PostMapping("/hello3")
    public Object hello3(@RequestBody Info info) {
        String msg = "DemoController recv args: id=" + info.toString() + ", date="
                + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS").format(new Date());
        return msg;
    }

    @PostMapping("/sendrmq")
    public Object rmq(@RequestBody Info info) {
        boolean sendResult = RocketMQSender.send(snatchOrderSendChannel, info);
        String msg = "DemoController recv args: sendResult=" + sendResult + ", date="
                + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS").format(new Date());
        return msg;
    }
}